package org.calconnect.icalproofing;

/**
 *	<p>StringUtils class</p>
 *
 *	<p>Simple string-related utilities.</p>
 *
 *	<p><em>Last checked in by: $Author: aron $ at $Date: 2008-10-23 15:23:16 -0700 (Thu, 23 Oct 2008) $ (UTC)</em></p>
 *
 *	@author Aron Roberts
 *	@version $Revision: 504 $
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class StringUtils {

	// Class variables

	// Get the name of this class
	final static String CLASSNAME = StringUtils.class.getName();

	// Create a log4j 'logger' for this class, based on the class name
  //
  // This allows us to identify which log events are coming from the
  // methods of this class
	public static Logger logger = Logger.getLogger( CLASSNAME );

	// Create a second log4j 'logger' for handling the test harness for this class,
	//
	// This allows us to specify individually how to handle the
	// the output from running tests in main()
	private static Logger testHarnessLogger = Logger.getLogger( "testHarnessLogger" );
	
	/////////////////////////////////////////////////////////////////
	/**
	 *	Class constructor (no argument).  This constructor is declared
	 *	private, to prevent object construction outside of this class,
	 *	since all of its methods are static.
	 */
	private StringUtils() {

		logger.debug("In no-argument constructor for " + CLASSNAME + "...");

	}

	/////////////////////////////////////////////////////////////////
	/**
	 *	Returns a String representing the value of an integer primitive ('int').
	 *
	 *	@param i		An integer primitive.
	 *
	 *	@return			A string representation of the
	 *							value of the integer primitive.
	 */
	public static String intToString( int i ) {
	
    logger.debug( "In intToString( int ) ... in class " + CLASSNAME );

		Integer integer = new Integer( i );
		return integer.toString();
		
	}

	/////////////////////////////////////////////////////////////////
	/**
	 *	Return an 'int' representing a String value.
	 *
	 *	@param str					A string representing an integer value.
	 *
	 *	@return							the int value of the string
	 */
	public static int stringToInt( String str ) {

    logger.debug( "In stringToInt( String ) ... in class " + CLASSNAME );
    
    int intValueOfString = 0;
    
    try {
			Integer integerValueOfString	= new Integer( str );
			intValueOfString							= integerValueOfString.intValue();
			
		} catch ( Exception e ) {
			logger.debug( "String '" + str + "' cannot be converted " + "to an int." );
			return intValueOfString;
		}
		
		return intValueOfString;

	}		

	/////////////////////////////////////////////////////////////////
	/**
	 *	Returns whether a string is empty (blank or null).
	 *	Whitespace is trimmed before performing this evaluation.
	 *
	 *	@param	s					A string.
	 *
	 *	@return 					True if the string is blank or null;
	 *										false if it is neither.
	 */
	public static boolean isEmpty( String s ) {

	  logger.debug( "In isEmpty( String ) ... in class " + CLASSNAME );

		try {
		
			if ( s == null || s.trim().equals( "" ) ) {
				return true;
			} else {
				return false;
			}
				
		// A NullPointerException here means that the string is null
		} catch( NullPointerException e ) {
			return true;
		}
		
	}	

	/////////////////////////////////////////////////////////////////
	/**
	 *	Returns whether a string is not empty (blank or null).
	 *
	 *	@param	s					A string.
	 *
	 *	@return 					True if the string is blank or null;
	 *										false if it is neither.
	 */
	public static boolean isNotEmpty( String s ) {

	  logger.debug( "In isNotEmpty( String ) ... in class " + CLASSNAME );

		boolean isNotEmpty = false;

		if ( ! isEmpty( s ) ) {
			isNotEmpty = true;
		}

		return isNotEmpty;
		
	}	

	/////////////////////////////////////////////////////////////////

  public static void main( String[] args ) {

		// Display descriptive information about this class

  	System.out.println( CLASSNAME );
  	System.out.println( "$Revision: 504 $" );
  	System.out.println( "$Date: 2008-10-23 15:23:16 -0700 (Thu, 23 Oct 2008) $" );
  	
  } // end of main() method
  	
}
